package opera;

import book.Book;
import book.BookList;

import java.util.Scanner;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 言初
 * Date: 2023-01-09
 * Time: 10:19
 */
public class DelOperation implements IOPeration{
    public void work(BookList bookList){
        System.out.println("删除图书");
        //先查找一下书架中是否有你想要删除的图书
        System.out.println("请输出你想要删除的图书:");
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入书名：");
        String name=sc.nextLine();
        sc.close();

        //检查这本书是否已经存在在书架中
        int indox=-1;
        int currentSize=bookList.getUsedSize();
        for(int i=0;i<currentSize;i++){
            Book tmp=bookList.getBook(i);
            if(tmp.getName().equals(name)){
                indox=i;
                break;
            }
        }
        if(indox==-1){
            System.out.println("书架中没有这本书");
            return;
        }
        //开始挪动数据
        for (int i = indox; i <currentSize-1 ; i++) {
            Book book=bookList.getBook(i+1);
            bookList.setBook(i,book);
        }

        //修改usedsize的值
        bookList.setUsedSize(currentSize-1);
        //因为数组中的元素是引用类型，它存储的是地址，所以删除操作应该把最后一个置为null
        bookList.setBook(currentSize-1,null);
        System.out.println("删除成功");
    }
}
